
// 注册命名空间
var DOTA2 = {};
DOTA2.passport = {};
//关闭窗口
window.top.document._closeWindow = function () {
	var scope = window.top.document;
	if (scope.getElementById('J_dialog_passport') != null) {
		var obj = scope.getElementById('J_dialog_passport');
		if (obj != null) {
			obj.parentNode.removeChild(obj);
		}
	}
	if (scope.getElementById('overlay01') != null) {
		var obj = scope.getElementById('overlay01');
		if (obj != null) {
			obj.style.display = 'none';
		}
	}
};
var $dialogPassport = $('#J_dialog_passport');
	$overlay01 = $('#overlay01');

var DOT2PASS = {
	init: function() {
		// 如果没有登录，确认用户是否同意说明
	},
	popupSign: function(url) {
		var iframesrc = $dialogPassport.find('iframe');
		$dialogPassport.find('iframe').attr('src', iframesrc.data('src'));
		$dialogPassport.attr('url', url);
		$dialogPassport.showPanelCenter();
		$overlay01.showOverlay();
		$.LB.init();
	},
	triggerAgreement: function(url) {
		var getPopupAgreement = this.popupAgreement();
		if (getPopupAgreement) {} else {
			location.href = url;
		}
		return;
	},
	popupAgreement: function() {
		if ($.cookie('isLogin') && !$.cookie('isAgreement')) {
			$J_agreement.showPanelCenter();
			$overlay01.showOverlay();
			return true;
		} else {
			return false;
		}
	}
};

var xhr_login = null;
var login_fn = function(_flag){
	/*console.log($.cookie('isLogin'));*/
	_flag = _flag ? _flag : '';
	if (xhr_login) xhr_login.abort();
	var oData = '';
	if ($.cookie('isLogin') == 1) {
		xhr_login = $.ajax({
			type: "post",
			async: true,
			data: oData,
			url: "http://passport.wanmei.com/sso/login?service=dota2_act&jsonpCallback=?",
			dataType: "jsonp",
			jsonp: "jsonpCallback", //服务端用于接收callback调用的function名的参数
			//jsonpCallback:"success_jsonpCallback", //callback的function名称
			success: function(res) {
				if (res.data == null) {
					$.cookie('isLogin', 0, {
						path: "/"
					});
					if(_flag === ''){
						DOT2PASS.popupSign(_flag);
					}
				}
			},
			error: function() {
				
			}
		}).always(function() {
			$dialogPassport.find('iframe').attr('src', '');
		});
	} else {
		DOT2PASS.popupSign(_flag);
	}
};

jQuery(function(){

	
	// sign in
	$('#js_login').on('click', function(event) {
		
				login_fn();
		
	});

	$dialogPassport.off().on('click', '.close-passport', function(event) {
		event.preventDefault();
		$dialogPassport.hide();
		$overlay01.hide();
	});
	
});